package com.note.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.note.app.annotation.LogRecord;
import com.note.app.dto.NoteCreateDTO;
import com.note.app.dto.NoteDTO;
import com.note.app.dto.NoteUpdateDTO;
import com.note.app.service.NoteService;
import com.note.common.context.UserContext;
import com.note.common.vo.ResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 笔记控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/notes")
@RequiredArgsConstructor
@Tag(name = "笔记管理", description = "笔记相关接口")
public class NoteController {

    private final NoteService noteService;

    @PostMapping
    @Operation(summary = "创建笔记", description = "创建一个新的笔记")
    @LogRecord(description = "创建笔记", module = "笔记管理", operationType = LogRecord.OperationType.CREATE)
    public ResponseVo<Long> createNote(@Valid @RequestBody NoteCreateDTO dto) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(noteService.createNote(userId, dto));
    }

    @PutMapping
    @Operation(summary = "更新笔记", description = "更新现有笔记的内容和属性")
    @LogRecord(description = "更新笔记", module = "笔记管理", operationType = LogRecord.OperationType.UPDATE)
    public ResponseVo<Long> updateNote(@Valid @RequestBody NoteUpdateDTO dto) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(noteService.updateNote(userId, dto));
    }

    @GetMapping("/{noteId}")
    @Operation(summary = "获取笔记详情", description = "根据笔记ID获取笔记详细信息")
    @LogRecord(description = "获取笔记详情", module = "笔记管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<NoteDTO> getNoteDetail(
            @Parameter(description = "笔记ID") @PathVariable Long noteId) {
        Long userId = UserContext.getUserId();
        // 增加浏览次数
        noteService.incrementViewCount(noteId);
        return ResponseVo.success(noteService.getNoteDetail(userId, noteId));
    }

    @DeleteMapping("/{noteId}")
    @Operation(summary = "删除笔记", description = "根据笔记ID删除笔记")
    @LogRecord(description = "删除笔记", module = "笔记管理", operationType = LogRecord.OperationType.DELETE)
    public ResponseVo<Boolean> deleteNote(
            @Parameter(description = "笔记ID") @PathVariable Long noteId) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(noteService.deleteNote(userId, noteId));
    }

    @GetMapping("/user")
    @Operation(summary = "获取用户笔记列表", description = "分页获取当前用户的笔记列表")
    public ResponseVo<Page<NoteDTO>> getUserNotes(
            @Parameter(description = "收藏夹ID") @RequestParam(required = false) Long collectionId,
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") long size) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(noteService.getUserNotes(userId, collectionId, current, size));
    }

    @GetMapping("/public")
    @Operation(summary = "获取公开笔记列表", description = "分页获取所有公开的笔记列表")
    public ResponseVo<Page<NoteDTO>> getPublicNotes(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") long size) {
        return ResponseVo.success(noteService.getPublicNotes(current, size));
    }

    @GetMapping("/{noteId}/history")
    @Operation(summary = "获取笔记历史版本", description = "获取笔记的所有历史版本号")
    public ResponseVo<List<Integer>> getNoteHistoryVersions(
            @Parameter(description = "笔记ID") @PathVariable Long noteId) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(noteService.getNoteHistoryVersions(userId, noteId));
    }

    @GetMapping("/{noteId}/version/{version}")
    @Operation(summary = "获取指定版本笔记", description = "获取笔记的指定历史版本内容")
    public ResponseVo<NoteDTO> getNoteByVersion(
            @Parameter(description = "笔记ID") @PathVariable Long noteId,
            @Parameter(description = "版本号") @PathVariable Integer version) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(noteService.getNoteByVersion(userId, noteId, version));
    }
} 